package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿相关接口")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    /**
     * 查询地址簿
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询地址簿")
    public Result<List<AddressBook>> list(){
        log.info("用户查询地址簿");
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    //@RequestBody注解是一个json对象 不能对单个基本类型的值进行接收
    public Result setDefaultAddress(@RequestBody AddressBook addressBook)
    {
        log.info("用户设置默认地址，地址id为：{}",addressBook.getId());
        addressBookService.setDefaultAddress(addressBook.getId());
        return Result.success();
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("用户查询默认地址")
    public Result queryDefaultAddress()
    {
        log.info("用户查询默认地址");
        AddressBook addressBook = addressBookService.queryDefaultAddress();
        return Result.success(addressBook);
    }

    /**
     * 新增地址
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result saveAddress(@RequestBody AddressBook addressBook)
    {
        log.info("用户新增地址，地址信息为：{}",addressBook);
        addressBookService.saveAddress(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddressById(Long id)
    {
        log.info("用户删除地址，地址id为：{}",id);
        addressBookService.deleteAddressById(id);
        return Result.success();
    }

    /**
     * 根据id查询购物车
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id)
    {
        log.info("用户查询地址，地址id为：{}",id);
        AddressBook address = addressBookService.getById(id);
        return Result.success(address);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook)
    {
        log.info("用户修改地址，地址信息为：{}",addressBook);
            addressBookService.updateAddress(addressBook);
            return Result.success();
    }
}
